package com.gitlab.lucky.weixin.auth;
import cn.hutool.core.util.ObjectUtil;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.gitlab.lucky.common.enums.Constant;
import com.gitlab.lucky.common.redis.RedisService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxOAuth2Service;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;


/**
 * @program: lucky
 * @author: Mr.Han
 * @create: 2018-11-04 16:50
 **/
@Slf4j
@Controller
@RequestMapping("auth")
public class WxAuthController {
    @Value("${wx.domain}")
    private String domain;
    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private RedisService redisService;

    @GetMapping("index")
    public String index() {
        return "wxpay";
    }

    @GetMapping("authPage")
    public String authPage() {
        WxOAuth2Service oAuth2Service = wxMpService.getOAuth2Service();
        String callbackUrl = domain+"/auth/callback";
        // 构建授权url
        String url = oAuth2Service.buildAuthorizationUrl(callbackUrl, WxConsts.OAuth2Scope.SNSAPI_BASE, null);
        return "redirect:"+url;
    }

    /**
     * 用户确认授权后的回调处理
     */
    @GetMapping("callback")
    public ModelAndView callback(String code) throws WxErrorException {
        WxOAuth2Service oAuth2Service = wxMpService.getOAuth2Service();
        // 利用code获取accessToken
        WxOAuth2AccessToken accessToken = oAuth2Service.getAccessToken(code);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("wxpay");
        mv.addObject("openId",accessToken.getOpenId());
        Object amount = redisService.get("amount");
        int i = Integer.parseInt(ObjectUtil.isNull(amount) ? String.valueOf(540) : amount.toString());
        String totalFee = BaseWxPayResult.fenToYuan(i);
        log.info("totalFee:[{}]",totalFee);
        mv.addObject("amount", totalFee);
        return mv;
    }
    @ResponseBody
    @GetMapping("createMenu")
    public String createMenu(String menuName) throws WxErrorException {
        // 创建菜单对象
        WxMenu menu = new WxMenu();
        // 创建按钮A
        WxMenuButton button = new WxMenuButton();
        button.setType(WxConsts.MenuButtonType.VIEW);
        button.setName(menuName);
        button.setUrl(domain+"/auth/authPage");
        // 将按钮A和按钮B添加到菜单
        menu.getButtons().add(button);
        // 创建按钮
        wxMpService.getMenuService().menuCreate(menu);
        return "OK";
    }
    @GetMapping("success")
    public String success() {
        return "success";
    }

}

